using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Recruiters_jobfolders : System.Web.UI.Page
{
     
    string connectionString = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        if(Session["rcuserid"] == null)
            Response.Redirect("login.aspx");
        connectionString =
         WebConfigurationManager.ConnectionStrings[
           "RecruitVietnam"].ConnectionString;
        if (!IsPostBack)
        {
            ViewState["SortDirection"] = "ASC";
            ShowFolder(); 
        } 
    }
     
     
    
    DataTable GetDataTable()
    {
        SqlServerProvider sqlServerProvider = new SqlServerProvider();
        sqlServerProvider.ConnectionString = connectionString;

        sqlServerProvider.ParameterCollection = new string[1] { "@RecruiterID"};
        sqlServerProvider.ValueCollection = new string[1] { Session["rcuserid"].ToString() };
        sqlServerProvider.CommandText = "SPViewFolders";
        sqlServerProvider.CommandType = CommandType.StoredProcedure;         
        DataTable dataTable = sqlServerProvider.GetDataTable();
        return dataTable;
    }
    DataView GetDataView(string columnName, string sortType)
    {
        SqlServerProvider sqlServerProvider = new SqlServerProvider();
        sqlServerProvider.ConnectionString = connectionString;
        sqlServerProvider.CommandText = "SPViewFolders";
        sqlServerProvider.ParameterCollection = new string[1] { "@RecruiterID"};
        sqlServerProvider.ValueCollection = new string[1] { Session["rcuserid"].ToString() };
        sqlServerProvider.CommandType = CommandType.StoredProcedure;
        DataTable dataTable = sqlServerProvider.GetDataTable();
        DataView dataView = new DataView(dataTable);
        dataView.Sort = columnName + " " + sortType;
        return dataView;
    }
 
    void ShowFolder()
    {
        try
        {
            GridViewHelper gridViewHelper = new GridViewHelper();
            gridViewHelper.FillDataTableToGridView(
            GetDataTable(),
            this.GridView1);  
            if (GridView1.Rows.Count == 0)
            {
                litError.Text = "Folder not found.";
                btnDelete.Visible = false;
            }
            else
            {
                literalTotal.Text = GridView1.Rows.Count.ToString();
                btnDelete.Visible = true;
                if (GridView1.Rows.Count < 10)
                    btnSave.Visible = true;     
                else
                    btnSave.Visible = false;                                
                litError.Text = "";
                 
            }
             
        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
    }
    void ShowFolder(string columnName, string sortType)
    {
        try
        {
            GridViewHelper gridViewHelper = new GridViewHelper();
            gridViewHelper.FillDataViewToGridView(
            GetDataView(columnName, sortType),
            this.GridView1); ;
            
            if (GridView1.Rows.Count == 0)
                litError.Text = "Folder not found.";
            else
                litError.Text = "";
        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
    }
     
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        ShowFolder(
        e.SortExpression, ViewState["SortDirection"].ToString());
        if (ViewState["SortDirection"].ToString() == "ASC")
            ViewState["SortDirection"] = "DESC";
        else
            ViewState["SortDirection"] = "ASC";
    }
     
     string GetSelectItem()
     {
         string selectItem = "";
         foreach (GridViewRow gridViewRow
         in GridView1.Rows)
         {
             CheckBox checkBox = (CheckBox)
             gridViewRow.FindControl("chkFolderID");
             
                if(checkBox.Checked)
                    selectItem +=
                     gridViewRow.Cells[1].Text + ",";
             
         }         
          
         return selectItem;
     }
    
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string deleteQuery = "";
        
        try
            {
            string selectItem = GetSelectItem(); 
                    
            if (selectItem != "")
            {
                selectItem = selectItem.Substring(
                0, selectItem.Length - 1);
                deleteQuery = "delete from " +
                                "tblRCFolders where FolderID in (" +
                                selectItem +
                                ") and RecruiterID = " +
                                Session["rcuserid"].ToString() + ";";
                RecruitVietnam rcv
                            = new RecruitVietnam();
                rcv.ConnectionString =
                connectionString;
                rcv.ExecuteSQL(deleteQuery);
                ShowFolder();
            }
        }
        catch (Exception ex)
        {
             litError.Text = ex.Message;
        }
        
    }



    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            
            if (txtName.Text != "")
            {                 
                RecruitVietnam rcv
                            = new RecruitVietnam();
                rcv.ConnectionString =
                connectionString;
                if(rcv.SaveFolder(Session["rcuserid"].ToString() ,
                txtName.Text, txtDescription.Text, txtManager.Text))
                {
                    txtName.Text= txtDescription.Text= txtManager.Text = "";
                    ShowFolder();                    
                }
                else
                    litError.Text = rcv.ErrorMessage;
            }
        }
        catch (Exception ex)
        {
            litError.Text = ex.Message;
        }
    }
}

